package com.cmcc.linlaker.ui.upload;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.android.volley.VolleyError;
import com.cmcc.linlaker.R;
import com.cmcc.linlaker.http.ApiDefine;
import com.cmcc.linlaker.ui.upload.adapter.UploadQuestionAdapter;
import com.cmcc.linlaker.ui.upload.bean.UploadQuestionBean;
import com.cmcc.linlaker.utils.BarUtils;
import com.cmcc.linlaker.utils.IntentUtils;
import com.cmcc.linlaker.volley.GsonRequestHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProblemListActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_history);

        BarUtils.initStatusBar(this);

        initView();
    }

    private UploadQuestionAdapter adapter;
    private RecyclerView recycler_upload;
    private List<UploadQuestionBean.UploadQuestionData> uploadList = new ArrayList<>();

    public void initView() {
        recycler_upload = findViewById(R.id.recycler_upload);
        setRecyclerView(recycler_upload, uploadList);
    }

    @Override
    protected void onResume() {
        super.onResume();

        getProblemList();
    }

    private void setRecyclerView(RecyclerView recycler_view, final List<UploadQuestionBean.UploadQuestionData> itemList) {
        recycler_view.setHasFixedSize(true);
        recycler_view.setLayoutManager(new LinearLayoutManager(this));

        recycler_view.setNestedScrollingEnabled(false);

        adapter = new UploadQuestionAdapter(this, itemList);
        adapter.setOnItemClickListener(new UploadQuestionAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(ProblemListActivity.this, UploadDetailActivity.class);
                intent.putExtra("problemBean", itemList.get(position));
                startActivity(intent);
            }
        });
        recycler_view.setAdapter(adapter);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

        }
    }

    public void back(View view) {
        finish();
    }

    /**
     * 获取列表
     */
    private void getProblemList() {
        GsonRequestHelper helper = new GsonRequestHelper(this);
        Map<String, String> params = new HashMap<>();

        helper.sendPOSTRequest(ApiDefine.PROBLEM_LIST, UploadQuestionBean.class, params, false, null);
        helper.setOnResponseListener(new GsonRequestHelper.OnResponseListener() {
            @Override
            public void onError(VolleyError error) {

            }

            @Override
            public void onSuccess(Object data) {
                if (data != null) {
                    UploadQuestionBean bean = (UploadQuestionBean) data;
                    if (bean.code.equals("0")) {
                        List<UploadQuestionBean.UploadQuestionData> list = bean.rows;
                        if (list != null && list.size() > 0) {
                            uploadList.clear();
                            uploadList.addAll(list);
                            adapter.notifyDataSetChanged();
                        }
                    }else if (bean.code.equals(IntentUtils.VAILD_CODE)) {
                        IntentUtils.skipToLogin(ProblemListActivity.this);
                    }
                }
            }
        });
    }

    /**
     * 新建上报
     */
    public void newUpload(View view) {
        Intent intent = new Intent(ProblemListActivity.this, NewUploadQuestionActivity.class);
        startActivity(intent);
    }

}
